#include <Adafruit_NeoPixel.h>

#define PIN D4         // 定义Neopixel数据引脚
#define NUMPIXELS 60   // 定义LED灯珠数量
#define BRIGHTNESS 30  // 设置亮度值 (0-255)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

//兑
int array1[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
//离
int array2[8] = { 9, 10, 11, 12, 13, 14, 15, 16 };
//震
int array3[7] = { 17, 18, 19, 20, 21, 22, 23 };
//坤
int array4[6] = { 24, 25, 26, 27, 28, 29 };
//艮
int array5[7] = { 30, 31, 32, 33, 34, 35, 36 };
//坎
int array6[7] = { 37, 38, 39, 40, 41, 42, 43 };
//巽
int array7[8] = { 44, 45, 46, 47, 48, 49, 50, 51 };
//乾
int array8[9] = { 52, 53, 54, 55, 56, 57, 58, 59, 60 };

//颜色
uint32_t red = strip.Color(255, 0, 0);        // 红色
uint32_t green = strip.Color(0, 255, 0);      // 绿色
uint32_t blue = strip.Color(0, 0, 255);       // 蓝色
uint32_t yellow = strip.Color(255, 255, 0);   // 黄色
uint32_t magenta = strip.Color(255, 0, 255);  // 品红
uint32_t cyan = strip.Color(0, 255, 255);     // 青色
uint32_t white = strip.Color(255, 255, 255);  // 白色
uint32_t orange = strip.Color(255, 165, 0);   // 橙色

int flag = 1;      //流程控制
int speed = 500;   //旋转速度
int my_index = 1;  //最终选中卦象

void setup() {
  Serial.begin(115200);
  strip.begin();
  strip.setBrightness(BRIGHTNESS);  // 设置亮度
  // strip.show();  // 初始化Neopixel
}

void loop() {
  if (flag == 1) {
    int random_number = random(1, 9);  // 生成一个1到8之间的随机数，注意上界是排他性的，所以用101来包含100
    my_index = random_number;
    while (speed > 0) {  // 当speed大于于0时继续循环
      printArray(array1, 8, red);
      printArray(array2, 8, green);
      printArray(array3, 7, blue);
      printArray(array4, 6, yellow);
      printArray(array5, 7, magenta);
      printArray(array6, 7, cyan);
      printArray(array7, 8, white);
      printArray(array8, 9, orange);
      speed = speed - 100;
      if (speed < 0) {
        speed = 0;
      }
    }
    flag = 2;
  }

  if (flag == 2) {
    switch (my_index) {
      case 1:
        showOne(array1, 8, red);
        break;
      case 2:
        showOne(array2, 8, green);
        break;
      case 3:
        showOne(array3, 7, blue);
        break;
      case 4:
        showOne(array4, 6, yellow);
        break;
      case 5:
        showOne(array5, 7, magenta);
        break;
      case 6:
        showOne(array6, 7, cyan);
        break;
      case 7:
        showOne(array7, 8, white);
        break;
      case 8:
        showOne(array8, 9, orange);
        break;
    }
  }
}

//亮完后关闭
void printArray(int arr[], int size, uint32_t color) {
  for (int j = 0; j < size; j++) {
    // Serial.print("size:");
    // Serial.println(size);
    // Serial.print("j:");
    // Serial.println(j);
    // Serial.print("arr:");
    // Serial.println(arr[j]);

    strip.setPixelColor(arr[j] - 1, color);
    strip.show();
  }
  delay(speed);
  for (int j = 0; j < size; j++) {
    strip.setPixelColor(arr[j] - 1, strip.Color(0, 0, 0));
    strip.show();
  }
}

//常亮
void showOne(int arr[], int size, uint32_t color) {
  for (int j = 0; j < size; j++) {
    strip.setPixelColor(arr[j] - 1, color);
    strip.show();
  }
}

// 获取数组的大小
int getArraySize(int arr[]) {
  return sizeof(arr) / sizeof(arr[0]);
}

void colorWipe(uint32_t color, int wait) {
  for (int i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, color);
    strip.show();
    delay(wait);
  }
}